﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace DDD4Fun.State
{
    public class MyStateMachine  
    {
        protected IState currentState;

        IState soldOutState;
        IState soldState;
        IState hasQuarterState;
        IState noQuarterState;
        IState winnerState;

        int count = 0;

        public MyStateMachine(int numberGumballs)
        {
            soldOutState = new SoldOutState(this);
            soldState = new SoldState(this);
            hasQuarterState = new HasQuarterState(this);
            noQuarterState = new NoQuarterState(this);
            winnerState = new WinnerState(this);

            count = numberGumballs;
            if (count > 0)
            {
                currentState = noQuarterState;
            }
        }

        public void SetState(IState state)
        {
            currentState = state;
        }

        public IState GetState()
        {
            return currentState;
        }

        public void InsertQuarter()
        {
            Thread.Sleep(1000);
            currentState.InsertQuarter();
        }

        public void EjectQuarter()
        {
            currentState.EjectQuarter();
        }

        public void TurnCrank()
        {
            currentState.TurnCrank();
            currentState.Dispense();
        }

        public void ReleaseBall()
        {
            Console.WriteLine("给一个糖果");
            if (count != 0)
                count -= 1;
        }

        public int GetCount()
        {
            return count;
        }

        public IState GetSoldOutState()
        {
            return soldOutState;
        }

        public IState GetSoldState()
        {
            return soldState;
        }

        public IState GetHasQuarterState()
        {
            return hasQuarterState;
        }

        public IState GetNoQuarterState()
        {
            return noQuarterState;
        }

        public IState GetWinnerState()
        {
            return winnerState;
        }

    }
}
